const fs = require('fs');
const path = require('path');
const glob = require('glob');

// 递归获取所有Vue文件
const getAllVueFiles = () => {
  return glob.sync('src/**/*.vue');
};

// 修复导入路径
const fixImportPath = (fileContent) => {
  // 将 @import 'styles/xxx 修改为 @import '@/styles/xxx
  return fileContent.replace(/@import\s+'styles\//g, "@import '@/styles/");
};

// 主函数
const main = () => {
  const files = getAllVueFiles();
  console.log(`找到 ${files.length} 个 Vue 文件`);
  
  let fixedCount = 0;
  
  files.forEach(file => {
    try {
      const filePath = path.resolve(file);
      const content = fs.readFileSync(filePath, 'utf8');
      
      if (content.includes("@import 'styles/")) {
        const fixedContent = fixImportPath(content);
        fs.writeFileSync(filePath, fixedContent, 'utf8');
        console.log(`修复文件: ${file}`);
        fixedCount++;
      }
    } catch (error) {
      console.error(`处理文件 ${file} 时出错:`, error);
    }
  });
  
  console.log(`共修复 ${fixedCount} 个文件的样式导入路径`);
};

main(); 